package com.aruba.bulethoothdemoapplication.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.aruba.bulethoothdemoapplication.databinding.FragmentPrintBinding;
import com.aruba.bulethoothdemoapplication.utils.livedatabus.LiveDataBus;
import com.aruba.bulethoothdemoapplication.utils.printer.PrinterStatus;
import com.aruba.bulethoothdemoapplication.viewmodel.PrintEventViewModel;
import com.aruba.bulethoothdemoapplication.viewmodel.SelectDeviceViewModel;

public class PrintFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        FragmentPrintBinding binding = FragmentPrintBinding.inflate(inflater, container, false);

        SelectDeviceViewModel selectDeviceViewModel = new ViewModelProvider(getActivity(), ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(SelectDeviceViewModel.class);

        PrintEventViewModel printEventViewModel = new ViewModelProvider(getActivity(), ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(PrintEventViewModel.class);
        printEventViewModel.setAddress(selectDeviceViewModel.getSelectBleDevice().getValue().getAddress());
        binding.setPrintEventViewModel(printEventViewModel);

        binding.setViewmodel(selectDeviceViewModel);
        binding.setLifecycleOwner(this);

        LiveDataBus.getInstance().with(PrinterStatus.EVENT_KEY, PrinterStatus.class).observe(this, new Observer<PrinterStatus>() {
            @Override
            public void onChanged(PrinterStatus printerStatus) {
                printEventViewModel.getPrintEventMsg().setValue(printerStatus);
            }
        });

        return binding.getRoot();
    }
}
